<?php

namespace app\index\service;

use app\index\model\ImportexportModel;
use Mpdf\Tag\Time;

class ImportexportService
{

    /**
     * 新增
     * @param int $module 模块
     * @param int $type 类型 1-导入 2-导出
     * @param string $operator 操作人
     * @param string $file_name 文件名称
     * @param string $source_file_url 源文件URL
     * @return int|bool
     */
    static public function addLogic($module, $type, $operator, $file_name = '', $source_file_url = '')
    {
        $data = [];
        $data['module'] = $module;
        $data['type'] = $type;
        $data['operator'] = $operator;
        $data['operate_time'] = date('Y-m-d H:i:s');
        $data['source_file_name'] = $file_name;
        $data['source_file_url'] = $source_file_url;
        $data['state'] = 1;
        return ImportexportModel::dbInsertImportexport($data);
    }

    /**
     * 处理
     * @param int $id 数据ID
     * @return bool
     */
    static public function handleLogic($id)
    {
        $update = [];
        $update['state'] = 2;
        return ImportexportModel::dbUpdateImportexport($id, $update);
    }

    /**
     * 完成
     * @param int $id 数据ID
     * @param int $result 1-全部成功 2-全部失败 3-解析失败
     * @param string $file_url 文件下载URL
     * @param string $file_name 文件名称
     * @return bool
     */
    static public function completeLogic($id, $result, $file_url = '', $file_name = '')
    {
        $data = ImportexportModel::dbGetImportexport($id);
        $type = $data['type'] ?? '';

        $update = [];
        $update['state'] = 3;
        $update['result'] = ($type == 2 && empty($file_url)) ? 2 : $result;
        !empty($file_url) && $update['file_url'] = $file_url;
        !empty($file_name) && $update['file_name'] = $file_name;
        $update['complete_time'] = date('Y-m-d H:i:s');

        return ImportexportModel::dbUpdateImportexport($id, $update);
    }

    /**
     * 导出
     * @param string $url 处理导出的URL
     * @param array $param 处理导出的参数
     * @param int $module 模块
     * @param string $operator 操作人
     * @return array
     */
    static public function exportLogic($param, $module, $url='')
    {
        if (empty($url)) {
            $url = config('skumanage') . 'index/Commonapi/exportAsync';
        }
        $id = self::addLogic($module, 2, UID);
        if (!$id) {
            return ['code' => '000010', 'msg' => '操作失败'];
        }
        $param['export_id'] = $id;
        curl($url, json_encode($param), array('Content-Type: application/json'), 'POST', 1);
        return ['code' => 1, 'msg' => '操作成功,跳转到队列查看进度','data'=>['url'=>'/#/Queue/index','target'=>1]];
    }

    //导入记录,type=2通过导入数据再导出
    static public function importTask($param, $module, $type = 1)
    {
        $param['cn_name']=CN_NAME;
        $param['uid']=UID;
        $param['_FILES']=$_FILES;
        if (empty($param['_FILES'])) {
            return ['code' => '000010', 'msg' => '上传文件不能为空'];
        }
/*        if(round($param['_FILES']['file']['size'] / 1024 / 1024, 2) > 3) {
            return ['code' => '000010', 'msg' => '上传文件不可大于3M'];
        }*/
        if (empty($param['actionAsync'])) {
            return ['code' => '000010', 'msg' => '处理动作不能为空'];
        }
        $ext=[
            'image/pjpeg','image/jpeg','image/gif','image/png','application/vnd.ms-excel'
        ];
        if(!in_array($param['_FILES']['file']['type'],$ext)){
            return ['code' => '000010', 'msg' => '文件格式不允许上传'];
        }
        $url = config('skumanage') . 'index/Commonapi/importAsync';
        $date=date('YmdHis');
        $ext=pathinfo($param['_FILES']['file']['name'])['extension'];
        $name=$date.'-'.UID.'.'.$ext;
        move_uploaded_file($param['_FILES']['file']['tmp_name'],FILE_PATH.'upload/'.$name);
        $param['_FILES']['file']['tmp_name']=FILE_PATH.'upload/'.$name;
        $id = self::addLogic($module, 1, $param['uid'], $param['_FILES']['file']['name'], '/attachment/upload/'.$name);
        $param['_FILES']['file']['name']=$name;
        if (!$id) {
            return ['code' => '000010', 'msg' => '操作失败'];
        }
        $param['importId'] = $id;
        curl($url, json_encode($param), array('Content-Type: application/json'), 'POST', 1);
        return ['code' => 1, 'msg' => '操作成功,跳转到队列查看进度','data'=>['url'=>'/#/Queue/index','target'=>1]];
    }

    /**
     * 获取导入导出列表
     */
    static public function getImportExportLists($param)
    {

    }
}
